//
//  BangDanListRecommendCell.m
//  danpin
//
//  Created by chuangjia on 31/7/15.
//  Copyright (c) 2015 chuangjia. All rights reserved.
//

#import "BangDanListRecommendCell.h"

#import "ZUIImageView.h"

@interface BangDanListRecommendCell ()
{
    
}
@property(nonatomic,strong)UIImageView *imagesView;
@property(nonatomic,strong)RTLabel *titleLabel;
@property(nonatomic,strong)RTLabel *brandLabel;

@property(nonatomic,strong)RTLabel *introLabel;


@property(nonatomic,strong)UIImageView *lineView;
@property(nonatomic,strong)UIButton *topBtn;
@property(nonatomic,strong)UIButton *delBtn;
@end
@implementation BangDanListRecommendCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.backgroundColor=[UIColor clearColor];
        _imagesView =[[UIImageView alloc] initWithFrame:CGRectZero];
        _imagesView.layer.cornerRadius=PHOTO_FRAME_WIDTH*1.5;
        _imagesView.layer.masksToBounds = YES;
        [self.contentView addSubview:_imagesView];
        
        _topBtn =[UIButton buttonWithType:UIButtonTypeCustom];
        _topBtn.backgroundColor =[UIColor clearColor];
        [_topBtn setImage:[UIImage imageNamed:@"ding_top.png"] forState:UIControlStateNormal];//给button添加image
        _topBtn.imageEdgeInsets = UIEdgeInsetsMake(0,10,21,_topBtn.titleLabel.bounds.size.width);
        _topBtn.titleLabel.font = [UIFont systemFontOfSize:16];//title字体大小
        _topBtn.titleLabel.textAlignment = NSTextAlignmentCenter;//设置title的字体居中
        [_topBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];//设置title在一般情况下为白色字体
        
        _topBtn.titleEdgeInsets = UIEdgeInsetsMake(17, -_topBtn.titleLabel.bounds.size.width-16, 0, 0);//设置title在button上的位置（上top，左left，下bottom，右right）
        [_topBtn addTarget:self action:@selector(topAction) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_topBtn];
        //
        _brandLabel =[[RTLabel alloc] initWithFrame:CGRectMake(0,0, DEVICE_WIDTH/2-PHOTO_FRAME_WIDTH*2,0)];
        _brandLabel.backgroundColor = [UIColor clearColor];
        _brandLabel.textColor =UIColorFromRGB(0xa5a5a5);
        _brandLabel.textAlignment = NSTextAlignmentLeft;
        _brandLabel.font = [UIFont systemFontOfSize:14];
        _brandLabel.lineSpacing =4;
        [self.contentView addSubview:_brandLabel];
        
        _titleLabel =[[RTLabel alloc] initWithFrame:CGRectMake(0,0, DEVICE_WIDTH/2-PHOTO_FRAME_WIDTH*2,0)];
        _titleLabel.backgroundColor = [UIColor clearColor];
        _titleLabel.textColor = UIColorFromRGB(0x285790);
        _titleLabel.textAlignment = NSTextAlignmentLeft;
        _titleLabel.font = [UIFont systemFontOfSize:14];
        _titleLabel.lineSpacing =4;
        [self.contentView addSubview:_titleLabel];
        
        _introLabel=[[RTLabel alloc] initWithFrame:CGRectMake(0,0, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*8,0)];
        _introLabel.backgroundColor = [UIColor clearColor];
        _introLabel.textColor = UIColorFromRGB(0xa5a5a5);
        _introLabel.textAlignment = NSTextAlignmentLeft;
        _introLabel.font = FONT(14);
        _introLabel.lineSpacing =4;
        [self.contentView addSubview:_introLabel];
        
      
        //
        _delBtn =[UIButton buttonWithType:UIButtonTypeCustom];
        _delBtn.titleLabel.font =[UIFont boldSystemFontOfSize:20];
        [_delBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [_delBtn setTitle:@"..." forState:UIControlStateNormal];
        [_delBtn addTarget:self action:@selector(delRankAction) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_delBtn];
        //
        _lineView =[[UIImageView alloc] initWithFrame:CGRectZero];
        _lineView.image =[UIImage imageNamed:@"line_xu.png"];
        [self.contentView addSubview:_lineView];
    }
    return self;
}
-(void)dealloc
{
    self.data=nil;
    self.imagesView.image=nil;
    self.imagesView=nil;
    self.topBtn =nil;
    self.titleLabel=nil;
    self.introLabel=nil;
    self.delBtn =nil;
    self.lineView=nil;
//    DLog(@"%s", __PRETTY_FUNCTION__);
}
- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    
    _delBtn.frame=CGRectMake(DEVICE_WIDTH - 50 -PHOTO_FRAME_WIDTH, self.bounds.size.height -MORE_BUTTON_HEIGHT-PHOTO_FRAME_WIDTH, 50, MORE_BUTTON_HEIGHT);
    if ([self.other intValue]==1) {
        self.lineView.frame =CGRectMake(0, self.bounds.size.height-1, DEVICE_WIDTH, 1);
    }
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    
}
/// 根据数据模型来显示内容
- (void)showInfo:(id)model other:(id)other key:(id)key indexPath:(NSIndexPath *)indexPath
{
    self.data=model;
    self.other=other;
    self.key=key;
    self.indexPath=indexPath;
    [self showInfo:model key:key indexPath:indexPath];
}
/// 根据数据模型来显示内容
- (void)showInfo:(id)model key:(id)key indexPath:(NSIndexPath *)indexPath
{
    self.indexPath=indexPath;
    self.key = key;
    NSDictionary *dict =(NSDictionary*)model;
    self.data =model;
    
    CGFloat fTop =PHOTO_FRAME_WIDTH+PHOTO_TIME_PADDING;
    self.topBtn.frame =CGRectMake(PHOTO_FRAME_WIDTH, fTop, PHOTO_FRAME_WIDTH*5, PHOTO_FRAME_WIDTH*5);
    NSString *topNum =[NSString stringWithFormat:@"%@",[dict objectForKey:@"rank_push_top"]];
    int hadTop =[[dict objectForKey:@"had_rank_push_top"] intValue];
    if (hadTop) {
        [_topBtn setTitleColor:UIColorFromRGB(0xd00202) forState:UIControlStateNormal];
    }else{
        [_topBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    }
    [self.topBtn setTitle:topNum forState:UIControlStateNormal];
    
   
    //time
    NSString *msg_date = [NSString stringWithFormat:@"%@",[dict objectForKey:@"upload_dateinfo"]];
    if (msg_date==nil||[msg_date isEqualToString:@"(null)"]) {
        msg_date=@"";
    }
    NSDictionary *attribute = @{NSFontAttributeName:FONT(14)};
    CGSize retSize = [msg_date boundingRectWithSize:CGSizeMake(DEVICE_WIDTH/2, 0)
                                            options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                         attributes:attribute
                                            context:nil].size;
   
    //
    id user =[dict objectForKey:@"user"];
    if (user && [user isKindOfClass:[NSDictionary class]]) {
        NSString *nick_name =[NSString stringWithFormat:@"%@",[user objectForKey:@"nick_name"]];
        NSString *largeFace =[NSString stringWithFormat:@"%@",[user objectForKey:@"largeFace"]];
        _imagesView.frame =CGRectMake(PHOTO_FRAME_WIDTH*7,fTop+PHOTO_FRAME_WIDTH, PHOTO_FRAME_WIDTH*3, PHOTO_FRAME_WIDTH*3);
        [_imagesView sd_setImageWithURL:[NSURL URLWithString:largeFace] placeholderImage:[UIImage imageNamed:@"user_face.png"]];
        [_titleLabel setText:nick_name];
        CGSize optimumSize = [_titleLabel optimumSize];
        CGFloat h1 =optimumSize.height;
        _titleLabel.frame =CGRectMake(PHOTO_FRAME_WIDTH*11, fTop+PHOTO_FRAME_WIDTH+PHOTO_TIME_PADDING,DEVICE_WIDTH-PHOTO_FRAME_WIDTH*12-retSize.width, h1);
    }
    //
    _brandLabel.frame =CGRectMake(DEVICE_WIDTH-retSize.width-PHOTO_FRAME_WIDTH, fTop+PHOTO_FRAME_WIDTH+PHOTO_TIME_PADDING, retSize.width, PHOTO_FRAME_WIDTH+PHOTO_TIME_PADDING);
    _brandLabel.text =msg_date;
    CGFloat h =_titleLabel.frame.size.height+PHOTO_TIME_PADDING+PHOTO_FRAME_WIDTH;
    if (h>PHOTO_FRAME_WIDTH*3+PHOTO_FRAME_WIDTH) {
        fTop =fTop+h;
    }else{
        fTop =fTop+PHOTO_FRAME_WIDTH*3+PHOTO_FRAME_WIDTH;
    }
    fTop=fTop+PHOTO_FRAME_WIDTH;
    //
    NSString *text =[NSString stringWithFormat:@"%@", [dict objectForKey:@"text"]];
    if (text.length>0) {
        text=[NSString stringWithFormat:@"<font size=17 color='#CCCCCC'><b>“ </b></font>%@<font size=17 color='#CCCCCC'><b> ”</b> </font>",text];
        [_introLabel setText:text];
        CGSize optimumSize1 = [_introLabel optimumSize];
        CGFloat h =optimumSize1.height;
        
        _introLabel.frame =CGRectMake(PHOTO_FRAME_WIDTH*7, fTop, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*8, h);
    }
    
    //
    [self layoutSubviews];
}
-(void)setCallBack:(void(^)(BEventType,UIView*,id,id,NSIndexPath *))callback
{
    self.actionBlock=callback;
}
-(void)commentAction:(id)sender
{
    if (self.actionBlock) {
        self.actionBlock(BEventType_comment,sender,self.data,self.key,self.indexPath);
    }
}
-(void)categoryAction:(id)sender
{
    if (self.actionBlock) {
        self.actionBlock(BEventType_category,sender,self.data,self.key,self.indexPath);
    }
}
-(void)topAction
{
    //    self.topBtn
    [UIView animateWithDuration:0.3 animations:^{
        self.topBtn.frame =CGRectMake(0, PHOTO_TIME_PADDING, PHOTO_FRAME_WIDTH*7, PHOTO_FRAME_WIDTH*7);
        self.topBtn.titleLabel.font =[UIFont systemFontOfSize:26];
    } completion:^(BOOL finished) {
        self.topBtn.frame =CGRectMake(PHOTO_FRAME_WIDTH, PHOTO_FRAME_WIDTH+PHOTO_TIME_PADDING, PHOTO_FRAME_WIDTH*5, PHOTO_FRAME_WIDTH*5);
        self.topBtn.titleLabel.font =[UIFont systemFontOfSize:16];
    }];
    if (self.actionBlock) {
        self.actionBlock(BEventType_bangdanTop,nil,self.data,self.key,self.indexPath);
    }
}
-(void)delRankAction
{
    if (self.actionBlock) {
        self.actionBlock(BEventType_bangdanDelDanpin,nil,self.data,self.key,self.indexPath);
    }
}
-(void)handleSingleTapFrom
{
    if (self.actionBlock) {
        self.actionBlock(BEventType_good,nil,self.data,self.key,self.indexPath);
    }
}
/// 返回Cell高度
+ (CGFloat)returnCellHeight:(id)model
{
    NSDictionary *dict =(NSDictionary*)model;
    CGFloat hight =PHOTO_FRAME_WIDTH*5;
    NSString *text =[NSString stringWithFormat:@"%@", [dict objectForKey:@"text"]];
    if (text.length>0) {
        RTLabel *comment =[[RTLabel alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH,0, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2,0)];
        comment.font = FONT(14);
        comment.lineSpacing =LSpacing;
        [comment setText:text];
        CGSize optimumSize1 = [comment optimumSize];
        hight = hight+optimumSize1.height+PHOTO_FRAME_WIDTH;
        
    }
    hight = hight+PHOTO_FRAME_WIDTH*10;
    return hight;
}

@end
